package com.yogee.topseller.activity;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.base.HttpActivity;
import com.yogee.topseller.bean.HuiyuanListModel;
import com.yogee.topseller.bean.Hymodel;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.http.BaseSubscriber1;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.LogUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/19.
 */

public class FankuiActivity extends HttpActivity{
    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.et_product_name)
    EditText et_product_name;
    @BindView(R.id.et_miaoshu)
    EditText et_miaoshu;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.tv_num)
    TextView tv_num;

    private String inputText="";

    private final String num = "/100";


    @Override
    protected int getLayoutId() {
        return R.layout.activity_fankui;
    }

    @Override
    protected void initView() {
        title.setTitle("问题反馈");
        title.setActivity(this);

        inputText = getIntent().getStringExtra("inputText");
        et_product_name.setText(inputText);
        try {
            et_product_name.setSelection(inputText.length());
        }catch (Exception e){

        }
        et_miaoshu.addTextChangedListener(mTextWatcher);
        et_phone.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                et_miaoshu.setFocusable(true);
                et_miaoshu.setFocusableInTouchMode(true);
                String ms = et_miaoshu.getText().toString();
                try {
                    et_miaoshu.setSelection(ms.length());
                }catch (Exception e){

                }
                return false;
            }
        });

        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.length()>=11){
                    hideAllInputMethod(FankuiActivity.this);
                }

            }
        });


        initdata();
    }

    private void initdata(){
        HttpManager.getInstance().viewInformationDay(AppUtil.getUserId(FankuiActivity.this)).compose(bindRecycler()).subscribe(new BaseSubscriber1(new HttpOnNextListener<Hymodel>() {
            @Override
            public void onNext(Hymodel o) {
                if(o.getResult().equals("1")){
                    et_phone.setText(o.getPhone());
                }
            }
        }, this));
    }
    @OnClick({R.id.tv_tijiao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_tijiao:
                String name = et_product_name.getText().toString();
                if(TextUtils.isEmpty(name)){
                    ToastUtils.showToast(FankuiActivity.this,"产品名称不能为空");
                    break;
                }
                String miaoshu = et_miaoshu.getText().toString();
                if(TextUtils.isEmpty(miaoshu)){
                    ToastUtils.showToast(FankuiActivity.this,"描述不能为空");
                    break;
                }
                String phone = et_phone.getText().toString();
                if(TextUtils.isEmpty(phone)){
                    ToastUtils.showToast(FankuiActivity.this,"手机号不能为空");
                    break;
                }

                if(!AppUtil.validatePhone(phone)){
                    ToastUtils.showToast(this, "请输入正确的手机号");
                    break;
                }
                fankui(name,miaoshu,phone);
                break;

        }
    }

    private void fankui(String proName,String miaoshu,String phone){
        if(pd!=null)
            pd.show();
        HttpManager.getInstance().userFeedBack(AppUtil.getUserId(FankuiActivity.this),proName,miaoshu,phone).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean>() {
            @Override
            public void onNext(ResultBean o) {
                if(pd!=null)
                    pd.dismiss();
                ToastUtils.showToast(FankuiActivity.this,"反馈成功");
                FankuiActivity.this.finish();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                LogUtils.e("sfz","错误信息"+e.toString());
                ToastUtils.showToast(FankuiActivity.this,ToastUtils.errorMsg1);
            }
        }, this));
    }

    @Override
    public void showMsg(String msg) {
        super.showMsg(msg);
        LogUtils.e("sfz","msg:"+msg);
        if(pd!=null)
            pd.dismiss();
    }


    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart;
        private int editEnd;

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            temp = s;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // mTextView.setText(s);//将输入的内容实时显示
        }

        @Override
        public void afterTextChanged(Editable s) {

            tv_num.setText(s.length() + num);

        }
    };



    public static void hideAllInputMethod(Activity act){
        InputMethodManager imm=(InputMethodManager)act.getSystemService(Context.INPUT_METHOD_SERVICE);
        //软键盘如果已经打开就要关闭
        if (imm.isActive()==true){
            imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
